﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using deckAnalyser.Support;

namespace deckAnalyser.Domain
{
  public class Card : DomainObject
  {
    public String Name;
    public String Type;
    public String Rarity;
    public String Cost;
    public String Text;
    public int CMC;

    public Card(String name)
    {
      Name = name;

      load();
      loadTypes();
      loadSubTypes();
      loadSuperTypes();
    }

    private void load()
    {
      DataTable dataTable = null;

      dataTable = DBWrapper.GetDataTable
      (
          " select m.cmc              as 'CMC'" +
          "       ,m.cost             as 'Cost'" +
          "       ,m.type             as 'Type'" +
          "       ,m.rarity           as 'Rarity'" +
          "       ,m.txt              as 'Text'" +
          " from   master.cards m" +
          " where  m.name = \"" + Name + "\""
      );

      if (dataTable.Rows.Count != 1) throw new Exception("To many cards selected");

      foreach (DataRow row in dataTable.Rows)
      {
        CMC = int.Parse(row["CMC"].ToString());
        Cost = row["Cost"] as String;
        Type = row["Type"] as String;
        Rarity = row["Rarity"] as String;
        Text = row["Text"] as String;



        if (Cost == null)
          Cost = "";
      }

    }

    private List<String> types = null;
    public List<String> Types
    {
      get { return types; }
    }
    private void loadTypes()
    {
      DataTable dataTable = null;

      dataTable = DBWrapper.GetDataTable
      (
          " select type               as 'Type'" +
          " from   master.cardtypes m" +
          " where  m.name = \"" + Name + "\""
      );

      types = (from row in dataTable.Rows.Cast<DataRow>() select row["Type"] as String).ToList<String>();
    }

    private List<String> subTypes = null;
    public List<String> SubTypes
    {
      get { return subTypes; }
    }
    private void loadSubTypes()
    {
      DataTable dataTable = null;

      dataTable = DBWrapper.GetDataTable
      (
          " select type               as 'Type'" +
          " from   master.cardsubtypes m" +
          " where  m.name = \"" + Name + "\""
      );

      subTypes = (from row in dataTable.Rows.Cast<DataRow>() select row["Type"] as String).ToList<String>();
    }

    private List<String> superTypes = null;
    public List<String> SuperTypes
    {
      get { return superTypes; }
    }
    private void loadSuperTypes()
    {
      DataTable dataTable = null;

      dataTable = DBWrapper.GetDataTable
      (
          " select type               as 'Type'" +
          " from   master.cardsupertypes m" +
          " where  m.name = \"" + Name + "\""
      );

      superTypes = (from row in dataTable.Rows.Cast<DataRow>() select row["Type"] as String).ToList<String>();
    }

    public bool IsLand
    {
      get { return types.Contains("Land"); }
    }

    public bool IsWhite { get { return Cost.Contains("W"); } }
    public bool IsBlue { get { return Cost.Contains("U"); } }
    public bool IsBlack { get { return Cost.Contains("B"); } }
    public bool IsRed { get { return Cost.Contains("R"); } }
    public bool IsGreen { get { return Cost.Contains("G"); } }
    public bool IsArtifact { get { return !IsWhite && !IsBlue && !IsBlack && !IsRed && !IsGreen; } }

    public override string Caption
    {
      get { return Name; }
    }
  }
}
